﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CircularClip : BaseObj
{

    float distanceTop;
    float distanceDown;
    float distanceLeft;
    float distanceRight;
    void Start()
    {
        base.Start();
        m_rigidbody.useGravity = false;
        m_rigidbody.isKinematic = true;
        m_collider.isTrigger = true;
    }

    public override void OnInit()
    {
        base.OnInit();
        if (mUpTrasnform != null)
            distanceTop = mUpTrasnform.position.y;
        if (mDownTrasnform != null)
            distanceDown = mDownTrasnform.position.y;
        if (mLeftTrasnform != null)
            distanceLeft = mLeftTrasnform.position.x;
        if (mRightTrasnform != null)
            distanceRight = mRightTrasnform.position.x;
    }
    public override void OnHandleTrigger(Transform camera, NoloDeviceType handleType)
    {
        base.OnHandleTrigger(camera, handleType);
        if (mUpTrasnform != null)
            distanceTop = mUpTrasnform.position.y;
        if (mDownTrasnform != null)
            distanceDown = mDownTrasnform.position.y;
        if (mLeftTrasnform != null)
            distanceLeft = mLeftTrasnform.position.x;
        if (mRightTrasnform != null)
            distanceRight = mRightTrasnform.position.x;
    }

    public override void OnReleseTrigger(NoloDeviceType handleType)
    {
        m_collider.isTrigger = true;
    }

    public override void OnHoldingTrigger(Ray newRay)
    {
        base.OnHoldingTrigger(newRay);
        Ray deviceRay = newRay;
        Vector3 hitPos;
        if (RayTriInsecPos(m_IntersectPlane[0], m_IntersectPlane[1], m_IntersectPlane[2], deviceRay, out hitPos))
        {
            transform.position = hitPos;
            if (transform.position.y <= distanceDown)
            {
                transform.position = new Vector3(transform.position.x, distanceDown, transform.position.z);
            }
            if (transform.position.x >= distanceRight)
            {
                transform.position = new Vector3(distanceRight, transform.position.y, transform.position.z);
            }
            if (transform.position.x <= distanceLeft)
            {
                transform.position = new Vector3(distanceLeft, transform.position.y, transform.position.z);
            }
            if (transform.position.y >= distanceTop)
            {
                transform.position = new Vector3(transform.position.x, distanceTop, transform.position.z);
            }

        }
    }
}
